package com.babo72.android_test.task;

import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.babo72.android_test.R;

public class LookupRunningTask extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		final Context mContext = this;
		setContentView(R.layout.running_task);
		final TextView tvResult = (TextView) this.findViewById(R.id.tvResult);
		
		Button btn = (Button) this.findViewById(R.id.btnGet);
		btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				ActivityManager activityManager = (ActivityManager) mContext.getSystemService(mContext.ACTIVITY_SERVICE);
				List<RunningTaskInfo> info = activityManager.getRunningTasks(7);
				StringBuilder sb = new StringBuilder();
				for (Iterator<RunningTaskInfo> iterator = info.iterator(); iterator.hasNext();) {
					RunningTaskInfo runningTaskInfo = iterator.next();
					sb.append(runningTaskInfo.topActivity.getClassName()).append("\n");
				}
				tvResult.setText(sb.toString());
			}
		});
	}
}
